package com.example.androidcoursedesign.main.tab;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidcoursedesign.R;
import com.example.androidcoursedesign.main.adapter.MyBannerAdapter;
import com.example.androidcoursedesign.main.adapter.SolarTermAdapter;
import com.example.androidcoursedesign.main.bean.BannerBean;
import com.example.androidcoursedesign.main.bean.BannerResponse;
import com.example.androidcoursedesign.main.bean.SolarTermResponse;
import com.google.gson.Gson;
import com.youth.banner.Banner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class Fragment1 extends Fragment {
   private RecyclerView myRecyclerView;
   private Banner banner;
   private SolarTermAdapter adapter;

   private OkHttpClient client;

   private Context mContext;
   private List<SolarTermResponse.ListBean> list;

   private List<BannerBean> bannerList;

   private MyBannerAdapter bannerAdapter;

   private Handler handler = new Handler(Looper.myLooper()){
       @Override
       public void handleMessage(@NonNull Message msg) {
           super.handleMessage(msg);
           switch (msg.what){
               case 100://成功
                   SolarTermResponse response= (SolarTermResponse) msg.obj;
                   list = response.getList();
                   adapter.setData(list);
                   adapter.notifyDataSetChanged();
                   break;
               case 101://失败
                   SolarTermResponse response2= (SolarTermResponse) msg.obj;
                   String msg1 = response2.getMsg();
                   Toast.makeText(mContext,msg1,Toast.LENGTH_SHORT).show();
                   break;
               case 102://Banner成功
                   bannerList= (List<BannerBean>) msg.obj;
                   bannerAdapter.setDatas(bannerList);
                   bannerAdapter.notifyDataSetChanged();
                   break;
           }
       }
   };




   public  void setContext(Context context){
       this.mContext=context;
   }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        client = new OkHttpClient();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1_layout, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        myRecyclerView = view.findViewById(R.id.myRecyclerView);
        banner=view.findViewById(R.id.banner);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        myRecyclerView.setLayoutManager(layoutManager);
        list = new ArrayList<>();
        adapter = new SolarTermAdapter(mContext,list);
        myRecyclerView.setAdapter(adapter);

        bannerList=new ArrayList<>();
        bannerAdapter=new MyBannerAdapter(bannerList,mContext);
        banner.setAdapter(bannerAdapter);
        initData();

        initBannerData();
    }

    private void initBannerData() {
        String url = "http://10.111.34.2:8085/home/bannerList";
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.i("Fragment1",e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

                String responseContent = response.body().string();

                Gson gson = new Gson();
                BannerResponse bannerResponse = gson.fromJson(responseContent, BannerResponse.class);
                String  code= bannerResponse.getCode();
                String msgContent=bannerResponse.getMsg();
//                String  code = (String) data.get("code");
//                String msgContent = (String) data.get("msg");

                //实例化消息实体
                Message msg = Message.obtain();
                if ("00".equals(code)){//成功
                    msg.obj=bannerResponse.getList();
                    msg.what=102;
                }else {//失败
                    msg.obj=msgContent;
                    msg.what=103;

                }
                //发送消息。因为请求返回的数据是在子线程，但数据展示会涉及到UI线程刷新，而刷新UI不能在子线程操作,所以这里使用handler实现异步通信
                handler.sendMessage(msg);
            }
        });
    }

    private void initData() {
        String url = "http://10.111.34.2:8085/home/solarTermList";
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.i("Fragment1",e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

                String responseContent = response.body().string();

                Gson gson = new Gson();
                SolarTermResponse solarTermResponse = gson.fromJson(responseContent, SolarTermResponse.class);

                //实例化消息实体
                Message msg = Message.obtain();
                if ("00".equals(solarTermResponse.getCode())){//成功
                  msg.obj=solarTermResponse;
                  msg.what=100;
                }else {//失败
                    msg.obj=solarTermResponse;
                    msg.what=101;

                }
                //发送消息。因为请求返回的数据是在子线程，但数据展示会涉及到UI线程刷新，而刷新UI不能在子线程操作,所以这里使用handler实现异步通信
                handler.sendMessage(msg);
            }
        });

    }
}
